import os
import json
from dotenv import load_dotenv
from pathlib import Path

# ========================= Configuration类 - 加载基础配置 =========================
class Configuration:
    """读取 .env 与 servers_config.json 等用以配置环境"""
    def __init__(self) -> None:
        # 加载环境变量
        load_dotenv(override=True)
        # 加载当前脚本绝对路径，用以跨模块调用
        self.current_dir = Path(__file__).resolve().parent
        # 静态文件路径
        self.folder_configs = self.current_dir / "configs"
        self.folder_prompts = self.current_dir / "prompts"
        self.folder_post_response_templates = self.current_dir / "post_response_templates"
        # 加载配置文件
        def get_config(config_path: Path):
            with open(config_path, 'r', encoding='utf-8') as file:
                return json.load(file)
        self.backend_configs = get_config(self.folder_configs / "backend_config.json")
        self.agent_configs = get_config(self.folder_configs / "agent_config.json")
        self.llm_configs = get_config(self.folder_configs / "llm_config.json")

        # 加载 导览页 请求的URL
        self.guide_page_post_url = self.backend_configs["GUIDE_PAGE_POST_URL"]
        # 加载 智能问答及思维链 请求的URL
        self.chat_post_url = self.backend_configs["CHAT_POST_URL"]
        self.chat_thinking_post_url = self.backend_configs["CHAT_THINKING_POST_URL"]
        # 加载 问答/图谱/统算/时序 请求的URL
        self.graph_post_url = self.backend_configs["GRAPH_POST_URL"]
        self.map_post_url = self.backend_configs["MAP_POST_URL"]
        self.stats_post_url = self.backend_configs["STATS_POST_URL"]
        self.time_series_post_url = self.backend_configs["TIME_SERIES_POST_URL"]
        # 加载 搜索框 请求的URL
        self.graph_searchbox_series_post_url = self.backend_configs["GRAPH_SEARCHBOX_POST_URL"]
        # 加载 AI点问 请求的URL
        self.click_to_ask_post_url = self.backend_configs["CLICK_TO_ASK_POST_URL"]
        # 加载 生成洞察请求 请求的URL
        self.insight_summary_post_url = self.backend_configs["INSIGHT_SUMMARY_POST_URL"]
        self.insight_graph_post_url = self.backend_configs["INSIGHT_GRAPH_POST_URL"]
        self.insight_map_post_url = self.backend_configs["INSIGHT_MAP_POST_URL"]
        self.insight_stats_post_url = self.backend_configs["INSIGHT_STATS_POST_URL"]
        self.insight_time_series_post_url = self.backend_configs["INSIGHT_TIME_SERIES_POST_URL"]

        # 加载 导览页 响应模版路径
        guide_page_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_GUIDE_PAGE_RESPONSE_TEMPLATE"]
        # 加载 问答/图谱/统算/时序请求 响应模版路径
        chat_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_CHAT_RESPONSE_TEMPLATE"]
        chat_thinking_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_CHAT_THINKING_RESPONSE_TEMPLATE"]
        graph_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_GRAPH_RESPONSE_TEMPLATE"]
        map_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_MAP_RESPONSE_TEMPLATE"]
        time_series_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_TIME_SERIES_RESPONSE_TEMPLATE"]
        stats_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_STATS_RESPONSE_TEMPLATE"]
        graph_searchbox_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_GRAPH_SEARCHBOX_RESPONSE_TEMPLATE"]
        click_to_ask_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_CLICK_TO_ASK_RESPONSE_TEMPLATE"]
        # 加载 生成洞察请求 响应模版路径
        insight_summary_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_INSIGHT_SUMMARY_RESPONSE_TEMPLATE"]
        insight_graph_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_INSIGHT_GRAPH_RESPONSE_TEMPLATE"]
        insight_map_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_INSIGHT_MAP_RESPONSE_TEMPLATE"]
        insight_stats_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_INSIGHT_STATS_RESPONSE_TEMPLATE"]
        insight_time_response_template_path = self.folder_post_response_templates / self.backend_configs[
            "PATH_INSIGHT_TIME_SERIES_RESPONSE_TEMPLATE"]

        # 加载 问答/图谱/统算/时序请求 响应模版
        def load_template(template_path: Path):
            with open(template_path, 'r',encoding='utf-8') as file:
                return json.load(file)

        self.guide_page_response_template = load_template(guide_page_response_template_path) # 导览页响应模板
        self.chat_response_template = load_template(chat_response_template_path) # Insight Agent 问答响应模板
        self.chat_thinking_response_template = load_template(chat_thinking_response_template_path)  # Insight Agent 思考响应模板
        self.graph_response_template = load_template(graph_response_template_path)  # 图谱响应模板
        self.map_response_template = load_template(map_response_template_path)  # 地图响应模板
        self.stats_response_template = load_template(stats_response_template_path)  # 统算响应模板
        self.time_series_response_template = load_template(time_series_response_template_path)  # 时序响应模板
        self.graph_searchbox_response_template = load_template(graph_searchbox_response_template_path)  # 图谱搜索响应模板
        self.click_to_ask_response_template = load_template(click_to_ask_response_template_path)  # AI点查响应模板

        self.insight_summary_response_template = load_template(insight_summary_response_template_path) # 生成洞察总结响应模板
        self.insight_graph_response_template = load_template(insight_graph_response_template_path) # 生成洞察图谱响应模板
        self.insight_map_response_template = load_template(insight_map_response_template_path) # 生成洞察地图响应模板
        self.insight_stats_response_template = load_template(insight_stats_response_template_path) # 生成洞察统算响应模板
        self.insight_time_response_template = load_template(insight_time_response_template_path) # 生成洞察时序响应模板

        # 加载大模型名称
        self.llm_model_name = self.llm_configs["LLM_MODEL"]
        # 加载 LangGraph Agent 配置文件
        self.main_agent_prompt_file_path = self.folder_prompts / self.agent_configs["MAIN_AGENT_PROMPT_PATH"]
        self.click_to_ask_agent_prompt_file_path = self.folder_prompts / self.agent_configs["CLICK_TO_ASK_AGENT_PROMPT_PATH"]
        # 加载 MCP Server 配置文件
        self.main_agent_mcp_servers_config_path = self.folder_configs / self.agent_configs["MCP_SERVERS_CONFIG_PATH"]
        # 配置对话线程
        self.thread_config_1 = {"configurable": {"thread_id": "thread1"}}
        self.thread_config_2 = {"configurable": {"thread_id": "thread2"}}
        self.thread_config_3 = {"configurable": {"thread_id": "thread3"}}

